package com.nononsenseapps.feeder.ui.home

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.view.ContextMenu
import android.view.MenuInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.nononsenseapps.feeder.db.ARG
import com.nononsenseapps.feeder.glide.GlideImageLoader
import com.nononsenseapps.feeder.ui.R
import com.nononsenseapps.feeder.model.FeedUnreadCount
import com.nononsenseapps.feeder.ui.EditFeedActivity
import com.nononsenseapps.feeder.ui.qr.GenerateQRCodeActivity
import com.nononsenseapps.feeder.util.*

class FeedHolder(private val onNavigationItemClickListener: OnNavigationItemClickListener, v: View) :
    RecyclerView.ViewHolder(v), View.OnClickListener,View.OnCreateContextMenuListener {
    val unreadCount: TextView = v.findViewById(R.id.feed_unreadcount)
    val title: TextView = v.findViewById(R.id.feed_name)
    var item: FeedUnreadCount? = null
    val feedThumb:ImageView=v.findViewById(R.id.feed_thumb)

    init {
        v.setOnClickListener(this)
        v.setOnCreateContextMenuListener(this)
    }

    fun showThumb(context:Context){
        item?.let{
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                feedThumb.setImageDrawable(context.getDrawable(R.drawable.ic_rss_feeds))
                if(!PrefUtils.isNightMode(context))
                   feedThumb.setColorFilter(Color.DKGRAY)
            }
            it.imageUrl?.let {url->
              feedThumb.clearColorFilter()
              GlideImageLoader.glide(context, url.toString(), true).into(feedThumb)
           }
        }
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    override fun onClick(v: View) {
        item?.let {
            onNavigationItemClickListener.onNavigationItemClick(
                    it.id, it.displayTitle, it.url.toString(), null
            )
        }
    }

    override fun onCreateContextMenu(menu: ContextMenu?, v: View?, menuInfo: ContextMenu.ContextMenuInfo?) {
        v?.context?.let { context ->
            if (menu != null) {
                MenuInflater(context).inflate(R.menu.contextmenu_feed_list, menu)

                menu.findItem(R.id.action_share_feed).setOnMenuItemClickListener {
                    item?.let{
                        generateQRCode(v.context,QRScanReturnType.FEED.buildContent(it.url.toString()),it.title+"\nfeed id:${it.id}")
                    }
                    true
                }

                menu.findItem(R.id.action_delete_feed).setOnMenuItemClickListener {
                    item?.let {
                        onNavigationItemClickListener.deleteFeed(it.id)
                    }
                    true
                }

                menu.findItem(R.id.action_edit_feed).setOnMenuItemClickListener {
                    item?.let {
                        val i = Intent(v.context, EditFeedActivity::class.java)
                        i.putExtra(ARG.FEED_ID, it.id)
                        v.context.startActivity(i)
                    }
                    true
                }

                menu.findItem(R.id.action_add_templated).setOnMenuItemClickListener {
                    item?.let {
                        val i = Intent(v.context, EditFeedActivity::class.java)
                        i.putExtra(ARG.FEED_ID, it.id)
                        i.putExtra(ARG.TEMPLATE, true)
                        v.context.startActivity(i)
                    }
                    true
                }

                menu.findItem(R.id.action_read_all).setOnMenuItemClickListener {
                    item?.let {
                        onNavigationItemClickListener.makeFeedAsRead(it.id)
                    }
                    true
                }

                menu.findItem(R.id.action_sync).setOnMenuItemClickListener {
                    item?.let {
                        onNavigationItemClickListener.syncFeedWithId(it.id)
                    }
                    true
                }

                menu.findItem(R.id.action_add_feed_rule).setOnMenuItemClickListener {
                    item?.let {
                        onNavigationItemClickListener.markAddFeedRule(it.id)
                    }
                    true
                }

                menu.findItem(R.id.action_feed_rule_able).setOnMenuItemClickListener {
                    item?.let {
                        onNavigationItemClickListener.markFeedRuleAble(it.id,true)
                    }
                    true
                }

                menu.findItem(R.id.action_feed_rule_un_able).setOnMenuItemClickListener {
                    item?.let {
                        onNavigationItemClickListener.markFeedRuleAble(it.id,false)
                    }
                    true
                }

                menu.findItem(R.id.action_feed_rule_list).setOnMenuItemClickListener {
                    item?.let {
                        onNavigationItemClickListener.showFeedRule(it.id)
                    }
                    true
                }

                menu.findItem(R.id.action_toggle_feed_read_able).setOnMenuItemClickListener {
                    item?.let {
                        onNavigationItemClickListener.toggleFeedReadable(it.id,true)
                        it.syncAble=true
                    }
                    true
                }
                menu.findItem(R.id.action_toggle_feed_unread_able).setOnMenuItemClickListener {
                    item?.let {
                        onNavigationItemClickListener.toggleFeedReadable(it.id,false)
                        it.syncAble=false
                    }
                    true
                }

                menu.findItem(R.id.action_attention_love_0).setOnMenuItemClickListener {
                    item?.let{
                        onNavigationItemClickListener.markFeedAttentionLevel(it.id,0)
                    }
                    true
                }

                menu.findItem(R.id.action_attention_love_1).setOnMenuItemClickListener {
                    item?.let{
                        onNavigationItemClickListener.markFeedAttentionLevel(it.id,1)
                    }
                    true
                }

                menu.findItem(R.id.action_attention_love_2).setOnMenuItemClickListener {
                    item?.let{
                        onNavigationItemClickListener.markFeedAttentionLevel(it.id,2)
                    }
                    true
                }

                menu.findItem(R.id.action_attention_love_3).setOnMenuItemClickListener {
                    item?.let{
                        onNavigationItemClickListener.markFeedAttentionLevel(it.id,3)
                    }
                    true
                }

                menu.findItem(R.id.action_attention_love_4).setOnMenuItemClickListener {
                    item?.let{
                        onNavigationItemClickListener.markFeedAttentionLevel(it.id,4)
                    }
                    true
                }

                val feedId= item!!.id
                menu.findItem(R.id.action_before_read_hour).setOnMenuItemClickListener {
                    val hourBefore = DateUtils.covertFormCurrentDate(DateUtils.COVENT_HOUR)
                    onNavigationItemClickListener.makeFeedAsRead(feedId,hourBefore)
                    true
                }
                menu.findItem(R.id.action_before_read_today).setOnMenuItemClickListener {
                    val hourBefore = DateUtils.covertFormCurrentDate(DateUtils.COVENT_DAY)
                    onNavigationItemClickListener.makeFeedAsRead(feedId,hourBefore)
                    true
                }
                menu.findItem(R.id.action_before_read_three_day).setOnMenuItemClickListener {
                    val hourBefore = DateUtils.covertFormCurrentDate(DateUtils.COVENT_THREE_DAY)
                    onNavigationItemClickListener.makeFeedAsRead(feedId,hourBefore)
                    true
                }
                menu.findItem(R.id.action_before_read_weak).setOnMenuItemClickListener {
                    val hourBefore = DateUtils.covertFormCurrentDate(DateUtils.COVENT_WEEK)
                    onNavigationItemClickListener.makeFeedAsRead(feedId,hourBefore)
                    true
                }
                menu.findItem(R.id.action_before_read_month).setOnMenuItemClickListener {
                    val hourBefore = DateUtils.covertFormCurrentDate(DateUtils.COVENT_MONTH)
                    onNavigationItemClickListener.makeFeedAsRead(feedId,hourBefore)
                    true
                }
                menu.findItem(R.id.action_before_read_custom).setOnMenuItemClickListener {
                    true
                }


                item?.let {
                    menu.findItem(R.id.action_attention_love_0).isEnabled = it.attentionLevel != 0
                    menu.findItem(R.id.action_attention_love_1).isEnabled = it.attentionLevel != 1
                    menu.findItem(R.id.action_attention_love_2).isEnabled = it.attentionLevel != 2
                    menu.findItem(R.id.action_attention_love_3).isEnabled = it.attentionLevel != 3
                    menu.findItem(R.id.action_attention_love_4).isEnabled = it.attentionLevel != 4

                    menu.findItem(R.id.action_toggle_feed_unread_able).isVisible = it.syncAble
                    menu.findItem(R.id.action_toggle_feed_read_able).setVisible(!it.syncAble)

                    menu.findItem(R.id.action_feed_rule_able).isVisible=!it.ruleAble
                    menu.findItem(R.id.action_feed_rule_un_able).isVisible=it.ruleAble
                }
            }
        }
    }


    fun generateQRCode(context: Context,url:String,title:String?=null){
        val i = Intent(context, GenerateQRCodeActivity::class.java)
        i.putExtra(ARG.LINK, url)
        title?.let { i.putExtra(ARG.TITLE, title) }
        context.startActivity(i)
    }
}
